import random, time
setGlobalDuration(-1)

beginPythonInst(1)
soundfont(duration=2)
endPythonInst()

proc = startCsound()

scl1 = scale('Em')
scl2 = scale('C')

def choose():
    global scl
    scl = random.choice([scl1,scl2])

choose()

rnd = loopseg(25,35)
rnd1 = loopseg(30,50)
rnd2 = loopseg(40,60)

time.sleep(2)
patterns = [[4,4,2], [4,2,2,1,1], [2,1,1,2,1,1,1,1], [3,2,2,3], [2,3,2,3], 
            [2,2,1,2,1,2], [2,1,1,1,1,2,1,1], [4,1,1,1,1,1,1]]

def pit(min,max):
    if ti1.getBeat() == 1 and (ti1.getBar() % 4) == 0:
        ti1.changePattern(random.choice(patterns))
    dict = {}
    dict['soundfont'] = {}
    dict['soundfont']['midipitch'] = scl[rnd.next()]
    dict['soundfont']['pan'] = random.randint(0,7)*0.125
    sendEvent(1, dict, proc)

def pit2():
    if ti2.getBeat() == 1 and (ti2.getBar() % 4) == 0:
        ti2.changePattern(random.choice(patterns))
    dict = {}
    dict['soundfont'] = {}
    dict['soundfont']['midipitch'] = scl[rnd1.next()]
    dict['soundfont']['pan'] = random.randint(0,7)*0.125
    sendEvent(1, dict, proc)

t3 = 0
def pit3():
    if ti3.getBeat() == 1 and (ti3.getBar() % 4) == 0:
        ti3.changePattern(random.choice(patterns))
    dict = {}
    dict['soundfont'] = {}
    dict['soundfont']['midipitch'] = scl[rnd2.next()]
    dict['soundfont']['pan'] = random.randint(0,7)*0.125
    sendEvent(1, dict, proc)

tempo = .125
ti1 = pattern(tempo, pit, [3,3,2,2], 24,36)
ti2 = pattern(tempo, pit2, [2,2,4,1,1])
ti3 = pattern(tempo, pit3, [1,2,2,2,1,2])
scaler = pattern(tempo, choose, [10,10,10,10])
ti1.start()
ti2.start()
ti3.start()
scaler.start()
ti1.play()
ti2.play()
ti3.play()
scaler.play()


